package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntryDAO;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.purchase.RCBudgetContract;
import com.shrct.businessprocess.purchase.RCIdentifiablePhysicalProductDeliverItem;
import com.shrct.businessprocess.purchase.RCProductDeliver;
import com.shrct.businessprocess.purchase.impl.RCIdentifiableProductDeliverItemImpl;
import com.shrct.businessprocess.purchase.proxy.RCIdentifiablePhysicalProductDeliverItemProxy;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Date;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCIDENTIFIABLEPHYSICALPRODUCTDELIVERITEM_TYPE")
public class RCIdentifiablePhysicalProductDeliverItemImpl extends RCIdentifiableProductDeliverItemImpl implements RCIdentifiablePhysicalProductDeliverItem {
  public RCIdentifiablePhysicalProductDeliverItemImpl() {
    super();
  }
  
  public RCIdentifiablePhysicalProductDeliverItemImpl(final RCProductDeliver deliver) {
    super( deliver);
    
  }
  
  public void performDeliver(final Date deliverDate) {
    RCProductEntry _product = this.getProduct();
    final RCIdentifiablePhysicalProductEntry p = ((RCIdentifiablePhysicalProductEntry) _product);
    BeanFactory _beanFactory = this.getBeanFactory();
    RCIdentifiablePhysicalProductEntryDAO _bean = _beanFactory.<RCIdentifiablePhysicalProductEntryDAO>getBean(RCIdentifiablePhysicalProductEntryDAO.class);
    RCProductDeliver _deliver = this.getDeliver();
    RCBudgetContract _contract = _deliver.getContract();
    RCBudget _budget = _contract.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    RCProductDeliver _deliver_1 = this.getDeliver();
    RCBudgetContract _contract_1 = _deliver_1.getContract();
    RCEnterprise _customer = _contract_1.getCustomer();
    _bean.transferPhysicalPruduct(_project, p, _customer, deliverDate);
    RCProductEntry _product_1 = this.getProduct();
    RCProductDeliver _deliver_2 = this.getDeliver();
    String _location = _deliver_2.getLocation();
    _product_1.setLocation(_location);
    RCProductEntry _product_2 = this.getProduct();
    RCProductDeliver _deliver_3 = this.getDeliver();
    String _branch = _deliver_3.getBranch();
    _product_2.setBranch(_branch);
    Iterable<RCIdentifiablePhysicalProductEntry> _allParts = p.allParts();
    final Procedure1<RCIdentifiablePhysicalProductEntry> _function = new Procedure1<RCIdentifiablePhysicalProductEntry>() {
        public void apply(final RCIdentifiablePhysicalProductEntry it) {
          RCEnterprise _enterprise = p.getEnterprise();
          it.setEnterprise(_enterprise);
          RCProductEntry _product = RCIdentifiablePhysicalProductDeliverItemImpl.this.getProduct();
          RCProductDeliver _deliver = RCIdentifiablePhysicalProductDeliverItemImpl.this.getDeliver();
          String _location = _deliver.getLocation();
          _product.setLocation(_location);
          RCProductEntry _product_1 = RCIdentifiablePhysicalProductDeliverItemImpl.this.getProduct();
          RCProductDeliver _deliver_1 = RCIdentifiablePhysicalProductDeliverItemImpl.this.getDeliver();
          String _branch = _deliver_1.getBranch();
          _product_1.setBranch(_branch);
        }
      };
    IterableExtensions.<RCIdentifiablePhysicalProductEntry>forEach(_allParts, _function);
  }
  
  public void cancelDeliver() {
    BeanFactory _beanFactory = this.getBeanFactory();
    RCIdentifiablePhysicalProductEntryDAO _bean = _beanFactory.<RCIdentifiablePhysicalProductEntryDAO>getBean(RCIdentifiablePhysicalProductEntryDAO.class);
    RCProductEntry _product = this.getProduct();
    _bean.cancelTransferPhysicalPruduct(((RCIdentifiablePhysicalProductEntry) _product));
    RCProductEntry _product_1 = this.getProduct();
    Iterable<RCIdentifiablePhysicalProductEntry> _allParts = ((RCIdentifiablePhysicalProductEntry) _product_1).allParts();
    final Procedure1<RCIdentifiablePhysicalProductEntry> _function = new Procedure1<RCIdentifiablePhysicalProductEntry>() {
        public void apply(final RCIdentifiablePhysicalProductEntry it) {
          RCProductEntry _product = RCIdentifiablePhysicalProductDeliverItemImpl.this.getProduct();
          RCEnterprise _enterprise = ((RCIdentifiablePhysicalProductEntry) _product).getEnterprise();
          it.setEnterprise(_enterprise);
          RCProductEntry _product_1 = RCIdentifiablePhysicalProductDeliverItemImpl.this.getProduct();
          String _location = ((RCIdentifiablePhysicalProductEntry) _product_1).getLocation();
          it.setLocation(_location);
        }
      };
    IterableExtensions.<RCIdentifiablePhysicalProductEntry>forEach(_allParts, _function);
  }
  
  public static RCIdentifiablePhysicalProductDeliverItem create(final RCProductDeliver deliver, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCIdentifiablePhysicalProductDeliverItem rCIdentifiablePhysicalProductDeliverItem = new com.shrct.businessprocess.purchase.impl.RCIdentifiablePhysicalProductDeliverItemImpl(
    	deliver
    );
    objectFactory.create(rCIdentifiablePhysicalProductDeliverItem);
    return rCIdentifiablePhysicalProductDeliverItem;			
    
  }
  
  public static RCIdentifiablePhysicalProductDeliverItem createRCIdentifiablePhysicalProductDeliverItemImpl(final RCProductDeliver deliver, final IObjectFactory objectFactory, final Procedure1<RCIdentifiablePhysicalProductDeliverItem> init) {
    com.shrct.businessprocess.purchase.RCIdentifiablePhysicalProductDeliverItem rCIdentifiablePhysicalProductDeliverItem = new com.shrct.businessprocess.purchase.impl.RCIdentifiablePhysicalProductDeliverItemImpl(
    	deliver
    );
    objectFactory.create(rCIdentifiablePhysicalProductDeliverItem);
    init.apply(rCIdentifiablePhysicalProductDeliverItem);
    
    return rCIdentifiablePhysicalProductDeliverItem;			
    
  }
  
  public RCIdentifiablePhysicalProductDeliverItemProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCIdentifiablePhysicalProductDeliverItemProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCIdentifiablePhysicalProductDeliverItemProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
